package net.sos.translator;

import java.util.Collection;

public class CollectionTranslatorImpl implements ICollectionTranslator {

   private ITranslator translator;

   @Override
   public void setTranslator(ITranslator translator) {
      this.translator = translator;
   }

   @Override
   public Collection<CharSequence> translate(
         Collection<? extends CharSequence> collection)
         throws TranslationException {
      Collection<CharSequence> newCollection = null;
      // Since this has to work with any Collection, we are
      // pretty much limited in our implementation choices. We can't use
      // iterators since they support the `update' method. We can't
      // use a `for' loop [inefficient as it might be for linked list
      // implementations] since there is no `get' method for Collections.
      // Oh well, since not all Collections support the access by index
      // semantics [like sets]; it's kinda OK. But the iterator update thing
      // surely sucks.
      try {
         newCollection = collection.getClass().newInstance();
         for (CharSequence seq : collection) {
            newCollection.add(translator.translate(seq.toString()));
         }
      } catch (Exception e) {
         throw new TranslationException(e.getMessage(), e);
      }
      return newCollection;
   }

}